package com.hdy.web.controller;

import com.hdy.web.dao.QuestionMapper;
import com.hdy.web.pojo.Question;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;

@Controller
public class InsertQController {
    final QuestionMapper questionMapper;
    public InsertQController(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }
    @RequestMapping("/postq")
    public String  InsertQ(@RequestParam("question") String question, HttpSession session){
        Question entity=new Question();
        String postId =(String) session.getAttribute("userName");
        questionMapper.postQ(postId,question,entity);
        return "redirect:getQ";
}
@RequestMapping("/getQ")
    public  String getQ(Model model){
    List<Question> lists = null;
    try {
        lists = questionMapper.getAll();
    } catch (Exception e) {
        return "plaza";
    }
    model.addAttribute("questions", lists);
    return "plaza";
}
@RequestMapping("/deleteQ")
public  String deleteQ(@RequestParam("qid")int id){
    int i = questionMapper.deleteQ(id);
    if (i==1){
        return "redirect:getQ";
    }
    return "404";
}
}


